//
//  YLContinuationOrderViewModel.m
//  YLFinanceService
//
//  Created by she1323 on 2017/10/25.
//  Copyright © 2017年 she1323. All rights reserved.
//

#import "YLContinuationOrderViewModel.h"

#import "WHNetworkTool.h"

#import "YLAccountTool.h"

#import "WHProgressHUD.h"

#import "YLContinuationOrderModel.h"


@implementation YLContinuationOrderViewModel

- (instancetype)init
{
    if (self = [super init]) {
        [self setUp];
    }
    return self;
}

- (void)setUp
{
    __weak typeof(self) weakSelf = self;
    _requestCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal * _Nonnull(id  _Nullable input) {
        
        return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
            
            [WHProgressHUD showStatus:@"正在加载可续投订单..."];
            NSString *url = [NSString stringWithFormat:@"%@AppCommon/PGetContinuedInvestmentOrderDto", YLURL];
            
            [[WHNetworkTool shareInstance] sendPostWithUrl:url header:[YLAccountTool header] params:nil success:^(id responseObject) {
                [WHProgressHUD dismiss];
                if ([responseObject[@"httpCode"] integerValue] == 200) {
                    YLContinuationOrderItem *item = [YLContinuationOrderItem yy_modelWithDictionary:responseObject[@"httpResult"]];
                    weakSelf.models = item.item;
                    [subscriber sendNext:nil];
                } else {
                    [WHProgressHUD showError:responseObject[@"message"]];
                }
                [subscriber sendCompleted];
            } fail:^(NSError *error) {
                [subscriber sendCompleted];
            }];
           
            return nil;
        }];
    }];
}

- (NSArray *)models
{
    if (!_models) {
        _models = [[NSArray alloc] init];
    }
    return _models;
}

- (void)dealloc
{
    [WHProgressHUD dismiss];
    [[WHNetworkTool shareInstance] cancel];
}

@end
